/*
 * @Description: 全宗上下文
 * @Author: Rfan
 * @Date: 2022-01-08 16:30:05
 * @LastEditTime: 2023-03-08 17:30:11
 */

import { createContext, useContext, useReducer } from 'react';
import {
  SET_TITLE,
  SET_SELECT_AT,
  SET_SELECT_FOND_ID,
  SET_SELECT_AT_ID,
  SET_CONDITION,
  SET_FIELDCONDITION,
  SET_ADDITIONALSEARCH,
  SET_LISTQUERYDATA,
  SET_CHOICE
} from '../constants';
import { ImageResultReducer } from '../reducers/ImageResultReducer';

const ImageResultContext = createContext();

const initialState = {
  title: '',
  atConditionList: {},
  fieldConditionList: [],
  additionalSearch: {},
  listQueryData: [],
  choices: {}
};

const ImageResultContextProvider = (props) => {
  const [state, dispatch] = useReducer(ImageResultReducer, initialState);

  const actions = {
    /**
     * @description: 获取和传出限定条件
     */
    getCondition(data) {
      console.log('context', data);
      dispatch({ type: SET_CONDITION, data });
    },
    getFieldConditionList(data) {
      dispatch({ type: SET_FIELDCONDITION, data });
    },
    // 整体赋值
    setAdditionalSearch(data) {
      dispatch({ type: SET_ADDITIONALSEARCH, data });
    },
    // 页码切换赋值
    getListQueryData(data) {
      dispatch({ type: SET_LISTQUERYDATA, data });
    },
    //
    setChoice(data) {
      console.log('12111');
      dispatch({ type: SET_CHOICE, data });
    },
    /**
     * @description: 设置搜索结果标题
     */
    setTitle(title) {
      dispatch({ type: SET_TITLE, title });
    },
    /**
     * @description: 设置选中的档案类型
     */
    setSelectAT(data) {
      dispatch({ type: SET_SELECT_AT, data });
    },

    /**
     * @description: 设置选中的全宗
     */
    setSelectFondId(id) {
      dispatch({ type: SET_SELECT_FOND_ID, id });
    },

    /**
     * @description: 设置选中的档案类型ID
     */
    setSelectATId(id) {
      dispatch({ type: SET_SELECT_AT_ID, id });
    }
  };

  return (
    <ImageResultContext.Provider value={{ ...state, ...actions }}>
      {props.children}
    </ImageResultContext.Provider>
  );
};

export const useImageResultContext = () => {
  return useContext(ImageResultContext);
};

export { ImageResultContext, ImageResultContextProvider };
